package scala.day01

import java.sql.{Connection, DriverManager, PreparedStatement, ResultSet}

object Demo08Jdbc {
  def main(args: Array[String]): Unit = {
    //加载驱动
    //com.mysql.jdbc.Driver
    Class.forName("com.mysql.jdbc.Driver")
    //创建链接
    val conn: Connection = DriverManager.getConnection("jdbc:mysql://master:3306/student","root","123456")
    //执行sql查询语句
    val stat: PreparedStatement = conn.prepareStatement("select * from student where clazz = ?")
    stat.setString(1,"文科一班")
    //执行查询返回结果
    val resultSet: ResultSet = stat.executeQuery()
    //解析数据
    while(resultSet.next()){
      //通过字段名读取数据
      val id: Int = resultSet.getInt("id")
      val name: String = resultSet.getString("name")
      val age: Int = resultSet.getInt("age")
      val clazz: String = resultSet.getString("clazz")
      val gender: String = resultSet.getString("gender")

      println(id + "\t" + name + "\t" + age + "\t" + gender + "\t" + clazz)
    }
    conn.close()
  }
}
